﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Sio.Mdm.BF.Idm;
using Sio.Mdm.Types.Database;
using Ext.Net;
using Sio.Mdm.Entities;
using Sio.Mdm.Client;
using Sio.Mdm.BF.Sifranti;
using Sio.Mdm.Types;
using System.Collections.Generic;

namespace Sio.Mdm.Web.Uporabnik
{
  public partial class StarsPodrobnosti : Controls.Page
  {
    private string Otrok(int vlogaID)
    {
        Ucec ucec = EntityFactory.LoadEntity<Ucec>
            (new Filter<Ucec>().Where(r => (r.Stars1 == vlogaID) || (r.Stars2 == vlogaID))
        );

        // If state is 'Unknown' entity was not found
        if (ucec.EntityState == EntityState.Unknown)
        {
            return String.Empty;
        }
        else
        {
            return String.Format("{0} {1}", ucec.Ime, ucec.Priimek);
        }
    }
    private string Razred(int razredID)
    { 
        RazredSkupina razred = EntityFactory.LoadEntity<RazredSkupina>(
            new Filter<RazredSkupina>().Where(r => r.RazredId == razredID)
        );

        return razred.EntityState == EntityState.Unknown ?
            String.Empty : razred.Oznaka;
    }

    private int RazredId(int vlogaId)
    { 
        UcecStatus status = EntityFactory.LoadEntity<UcecStatus>(
            new Filter<UcecStatus>().Where(r => r.VlogaId == vlogaId)
        );

        return status.EntityState == EntityState.Unknown ? 0 : status.RazredId;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !X.IsAjaxRequest)
      {
        int stars = Request.GetValue<int>("stars"); 
        //string podrobnosti = "";
        //int vlogaId = 0;
        
          //starš1
        Stars EStars = EntityFactory.LoadEntity<Stars>(
          new Filter<Stars>().Where(r => (r.VlogaId == stars))
        );

        if (EStars.EntityState == EntityState.Unchanged)
        {
          this.tfTelefon.Value = EStars.Telefon;
          this.tfTelefonSluzba.Value = EStars.TelefonSluzba;
          this.tfEmail.Value = EStars.Ime + "." + EStars.Priimek + "@sio.si";
        }

        //otroci
        Ucec uceci = EntityFactory.LoadEntity<Ucec>(
          new Filter<Ucec>().Where(r => (r.Stars1 == stars) || (r.Stars2 == stars))
        );
       
        DataTable table = new DataTable();

        table.Columns.AddRange(new DataColumn[] {
            new DataColumn("VlogaId")   { ColumnName = "VlogaId",    DataType = typeof(int) },
            new DataColumn("IdmStevilka")   { ColumnName = "IdmStevilka",    DataType = typeof(int) },
            new DataColumn("Otrok")   { ColumnName = "Otrok",    DataType = typeof(string) },         
            new DataColumn("DatumRojstva")     { ColumnName = "DatumRojstva",      DataType = typeof(DateTime) },
            new DataColumn("Razred")   { ColumnName = "Razred",    DataType = typeof(string) },  
          });
        if (uceci.EntityState == EntityState.Unchanged)
        {
          DataRow myRow;
          myRow = table.NewRow();
          myRow["VlogaId"] = uceci.VlogaId;
          myRow["Otrok"] = uceci.Ime + " " + uceci.Priimek;
          myRow["Razred"] = Razred(RazredId(uceci.VlogaId));
          myRow["DatumRojstva"] = DateTime.Now;
          //podrobnosti = uceci.Ime + " " + uceci.Priimek;
          //vlogaId = uceci.VlogaId;
          //this.OtrociGrid.Listeners.Click.Handler = "parent.addTab(parent.TabNavigation, 'idPodrobnosti1', 'UceciPodrobnosti.aspx?vlogaId=" + vlogaId + "','Podrobnosti " + podrobnosti + "');"; 

          table.Rows.Add(myRow);
        }

        StoreOtrociSeznam.DataSource = table;
        StoreOtrociSeznam.DataBind();
      }
    }
  }
}
